/**
 * @Author MrsGuo
 * version 1.0
 * @Date 2023-08-04 16:02
 */
package com.cl.erp_2301.controller;

import com.cl.erp_2301.Service.UpdatePwdService;
import com.cl.erp_2301.entity.Emp;
import com.cl.erp_2301.util.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/v2")
public class UpdatePwdController {
    @Resource
    private UpdatePwdService updatePwdService;
    @PostMapping("/updatepwd")
    public Result updatePwd(@RequestParam("oldPwd") String oldPassword , @RequestParam("newPwd") String newPassword, HttpServletRequest request, HttpServletResponse response){
        Integer uuid=(Integer) request.getSession().getAttribute("uuid");
        Emp empById=updatePwdService.getEmpById(uuid);
        if (!(newPassword.equals(empById.getPassword()))){
            updatePwdService.updatePwd(newPassword,uuid);
            return Result.ok();
        }
        else if(newPassword.equals(empById.getPassword())){
            return Result.error(401,"旧密码与新密码相同");
        }

            return Result.error(401,"旧密码错误");
    }
    @GetMapping("//login_loginOut")
    public Result logout(HttpServletRequest request){
        request.getSession().removeAttribute("username");
        request.getSession().removeAttribute("uuid");
        return Result.ok();
    }

}
